package com.itrus.svm;

import com.itrus.cryptorole.CryptoException;
import com.itrus.cryptorole.SignatureVerifyException;
import com.itrus.util.DERUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Signature;
import java.security.SignatureException;
import java.security.cert.CertificateParsingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1OctetString;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.ASN1Set;
import org.bouncycastle.asn1.DEREncodable;
import org.bouncycastle.asn1.DERInteger;
import org.bouncycastle.asn1.DERObject;
import org.bouncycastle.asn1.DERObjectIdentifier;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.pkcs.IssuerAndSerialNumber;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.asn1.pkcs.SignedData;
import org.bouncycastle.asn1.pkcs.SignerInfo;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.asn1.x509.X509CertificateStructure;
import org.bouncycastle.jce.X509Principal;
import org.bouncycastle.util.encoders.Base64;

public class SVM
  implements PKCSObjectIdentifiers
{
  private static final String ID_DSA = "1.2.840.10040.4.1";
  private static final String ID_MD2 = "1.2.840.113549.2.2";
  private static final String ID_MD5 = "1.2.840.113549.2.5";
  private static final String ID_PKCS7_DATA = "1.2.840.113549.1.7.1";
  private static final String ID_PKCS7_SIGNED_DATA = "1.2.840.113549.1.7.2";
  private static final String ID_RSA = "1.2.840.113549.1.1.1";
  private static final String ID_SHA1 = "1.3.14.3.2.26";

  public static byte[] fetchEncryptedDigest(String paramString1, String paramString2)
    throws SignatureVerifyException, CryptoException
  {
    byte[] arrayOfByte1 = paramString1.getBytes();
    ((byte[])null);
    if (!paramString2.startsWith("M"));
    for (byte[] arrayOfByte2 = DERUtils.HexStringToBytes(paramString2); ; arrayOfByte2 = Base64.decode(paramString2))
      return verifyAndParsePkcs7(arrayOfByte1, arrayOfByte2).getEncryptedDigest();
  }

  private static String getDigestAlgorithm(String paramString1, String paramString2)
  {
    String str1 = paramString1;
    String str2 = paramString2;
    if (paramString1.equals("1.2.840.113549.2.5"))
    {
      str1 = "MD5";
      if (!paramString2.equals("1.2.840.113549.1.1.1"))
        break label82;
      str2 = "RSA";
    }
    while (true)
    {
      return str1 + "with" + str2;
      if (paramString1.equals("1.2.840.113549.2.2"))
      {
        str1 = "MD2";
        break;
      }
      if (!paramString1.equals("1.3.14.3.2.26"))
        break;
      str1 = "SHA1";
      break;
      label82: if (paramString2.equals("1.2.840.10040.4.1"))
        str2 = "DSA";
    }
  }

  public static SignerAndEncryptedDigest verifyAndParsePkcs7(byte[] paramArrayOfByte1, byte[] paramArrayOfByte2)
    throws SignatureVerifyException, CryptoException
  {
    ASN1InputStream localASN1InputStream = new ASN1InputStream(paramArrayOfByte2);
    DERObject localDERObject;
    try
    {
      localDERObject = localASN1InputStream.readObject();
      if (!(localDERObject instanceof ASN1Sequence))
        throw new SecurityException("Not a valid PKCS#7 object - not a sequence");
    }
    catch (IOException localIOException)
    {
      throw new SecurityException("can't decode PKCS7SignedData object");
    }
    org.bouncycastle.asn1.cms.ContentInfo localContentInfo = org.bouncycastle.asn1.cms.ContentInfo.getInstance(localDERObject);
    if (!localContentInfo.getContentType().equals(signedData))
      throw new SecurityException("Not a valid PKCS#7 signed-data object - wrong header " + localContentInfo.getContentType().getId());
    SignedData localSignedData = SignedData.getInstance(localContentInfo.getContent());
    ArrayList localArrayList = new ArrayList();
    Enumeration localEnumeration;
    if (localSignedData.getCertificates() != null)
      localEnumeration = ASN1Set.getInstance(localSignedData.getCertificates()).getObjects();
    ASN1Set localASN1Set;
    while (true)
      if (!localEnumeration.hasMoreElements())
      {
        DEREncodable localDEREncodable = localSignedData.getContentInfo().getContent();
        if (localDEREncodable != null)
          paramArrayOfByte1 = ((DEROctetString)localDEREncodable).getOctets();
        localASN1Set = localSignedData.getSignerInfos();
        if (localASN1Set.size() != 1)
          throw new SecurityException("This PKCS#7 object has multiple SignerInfos - only one is supported at this time");
      }
      else
      {
        try
        {
          localArrayList.add(new com.itrus.cert.X509Certificate(X509CertificateStructure.getInstance(localEnumeration.nextElement())));
        }
        catch (CertificateParsingException localCertificateParsingException)
        {
          CryptoException localCryptoException4 = new CryptoException(localCertificateParsingException);
          throw localCryptoException4;
        }
      }
    SignerInfo localSignerInfo = SignerInfo.getInstance(localASN1Set.getObjectAt(0));
    IssuerAndSerialNumber localIssuerAndSerialNumber = localSignerInfo.getIssuerAndSerialNumber();
    BigInteger localBigInteger = localIssuerAndSerialNumber.getCertificateSerialNumber().getValue();
    X509Principal localX509Principal = new X509Principal(localIssuerAndSerialNumber.getName());
    Iterator localIterator = localArrayList.iterator();
    boolean bool = localIterator.hasNext();
    Object localObject = null;
    if (!bool);
    while (true)
    {
      if (localObject != null)
        break label389;
      throw new SecurityException("Can't find signing certificate with serial " + localBigInteger.toString(16));
      com.itrus.cert.X509Certificate localX509Certificate = (com.itrus.cert.X509Certificate)localIterator.next();
      if ((!localBigInteger.equals(localX509Certificate.getSerialNumber())) || (!localX509Principal.equals(localX509Certificate.getIssuerDN())))
        break;
      localObject = localX509Certificate;
    }
    label389: String str1 = localSignerInfo.getDigestAlgorithm().getObjectId().getId();
    byte[] arrayOfByte = localSignerInfo.getEncryptedDigest().getOctets();
    String str2 = localSignerInfo.getDigestEncryptionAlgorithm().getObjectId().getId();
    try
    {
      localSignature = Signature.getInstance(getDigestAlgorithm(str1, str2));
      localSignature.initVerify(localObject.getPublicKey());
      if (paramArrayOfByte1 == null)
        throw new CryptoException("Unable to get the original data to be signed.");
    }
    catch (NoSuchAlgorithmException localNoSuchAlgorithmException)
    {
      Signature localSignature;
      CryptoException localCryptoException3 = new CryptoException(localNoSuchAlgorithmException);
      throw localCryptoException3;
      localSignature.update(paramArrayOfByte1);
      if (!localSignature.verify(arrayOfByte))
      {
        SignatureVerifyException localSignatureVerifyException = new SignatureVerifyException("Signature verify failed, plaintext may be falsified. Signer is [" + localObject.getSubjectDNString() + "]");
        localSignatureVerifyException.setSigner(localObject);
        throw localSignatureVerifyException;
      }
    }
    catch (InvalidKeyException localInvalidKeyException)
    {
      CryptoException localCryptoException2 = new CryptoException(localInvalidKeyException);
      throw localCryptoException2;
      SignerAndEncryptedDigest localSignerAndEncryptedDigest = new SignerAndEncryptedDigest();
      localSignerAndEncryptedDigest.setSigner(localObject);
      localSignerAndEncryptedDigest.setEncryptedDigest(arrayOfByte);
      localSignerAndEncryptedDigest.setOriData(paramArrayOfByte1);
      return localSignerAndEncryptedDigest;
    }
    catch (SignatureException localSignatureException)
    {
      CryptoException localCryptoException1 = new CryptoException(localSignatureException);
      throw localCryptoException1;
    }
  }

  public static java.security.cert.X509Certificate verifySignature(String paramString1, String paramString2)
    throws SignatureVerifyException, CryptoException
  {
    try
    {
      java.security.cert.X509Certificate localX509Certificate = verifySignature(paramString1.getBytes("iso8859-1"), paramString2);
      return localX509Certificate;
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
      localUnsupportedEncodingException.printStackTrace();
    }
    return null;
  }

  public static java.security.cert.X509Certificate verifySignature(byte[] paramArrayOfByte, String paramString)
    throws SignatureVerifyException, CryptoException
  {
    ((byte[])null);
    if (!paramString.startsWith("M"));
    for (byte[] arrayOfByte = DERUtils.HexStringToBytes(paramString); ; arrayOfByte = Base64.decode(paramString))
      return verifyAndParsePkcs7(paramArrayOfByte, arrayOfByte).getSigner();
  }
}

/* Location:           C:\Users\user\Desktop\dd28_pcdd720\classes_dex2jar.jar
 * Qualified Name:     com.itrus.svm.SVM
 * JD-Core Version:    0.6.2
 */